using MicroMsg.Common.Event;
using MicroMsg.Manager;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.UserContrl
{
	public class ProfileAblumCtrl : UserControl
	{
		public delegate void ListItemClick(object sender, object object_0);
		private const string TAG = "ProfileAblumCtrl";
		private const int cNMaxCacheCnt = 3;
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(Contact), typeof(ProfileAblumCtrl), new PropertyMetadata(null, new PropertyChangedCallback(ProfileAblumCtrl.OnItemObjectPropertyChanged)));
		private bool mBIsFirstLoaded = true;
		private EventWatcher mWatchAblum;
		private object mObjectWatcher = new object();
		private int mNCnt;
		private static List<ProfileAblumCtrl> sItemsCached = new List<ProfileAblumCtrl>();
		internal Button btnLayoutRoot;
		internal StackPanel panelPics;
		internal CirclePagePicItem pic1;
		internal CirclePagePicItem pic2;
		internal CirclePagePicItem pic3;
		private bool _contentLoaded;
		public event ProfileAblumCtrl.ListItemClick ItemClick;
		public Contact ItemObject
		{
			get
			{
				return (Contact)base.GetValue(ProfileAblumCtrl.ItemObjectProperty);
			}
			set
			{
				base.SetValue(ProfileAblumCtrl.ItemObjectProperty, value);
			}
		}
		public ProfileAblumCtrl()
		{
			base.set_Margin(new Thickness(0.0, 16.0, 0.0, 6.0));
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
			this.pic1.DisableTouchEffect();
			this.pic2.DisableTouchEffect();
			this.pic3.DisableTouchEffect();
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == ProfileAblumCtrl.ItemObjectProperty)
			{
				(dependencyObject_0 as ProfileAblumCtrl).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				this.RefreshItem();
			}
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void registerEvent()
		{
			if (this.mWatchAblum == null)
			{
				this.mWatchAblum = new EventWatcher(this.mObjectWatcher, null, new EventHandlerDelegate(this.OnAblum));
				EventCenter.registerEventWatcher(EventConst.ON_MGR_SNSPAGE_GET_LAST_IMAGE, this.mWatchAblum);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchAblum != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_MGR_SNSPAGE_GET_LAST_IMAGE, this.mWatchAblum);
				this.mWatchAblum = null;
			}
		}
		private void OnAblum(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			EventConst mEventID = evtArgs.mEventID;
			if (mEventID != EventConst.ON_MGR_SNSPAGE_GET_LAST_IMAGE)
			{
				return;
			}
			if (evtArgs.mObject != null)
			{
				string text = evtArgs.mObject as string;
				if (string.IsNullOrEmpty(text))
				{
					return;
				}
				if (this.ItemObject == null || text != this.ItemObject.strUsrName)
				{
					return;
				}
				this.unRegisterEvent();
				List<TloMedia> listMideas = evtArgs.mObject1 as List<TloMedia>;
				this.showPics(listMideas);
			}
		}
		private void btnLayoutRoot_Click(object sender, RoutedEventArgs e)
		{
			if (this.ItemClick != null)
			{
				this.ItemClick(this, this.ItemObject);
			}
			BasePage rootPage = ToolFunction.GetRootPage();
			BasePage prevPage = PageManager.getInstance().GetPrevPage();
			if (rootPage is LbsPeopleInfo)
			{
				LbsPeopleInfo lbsPeopleInfo = rootPage as LbsPeopleInfo;
				if (lbsPeopleInfo != null && lbsPeopleInfo.mLbsPeople != null)
				{
					if (prevPage is SnsUserMainPage)
					{
						SnsUserMainPage snsUserMainPage = prevPage as SnsUserMainPage;
						if (snsUserMainPage.mLbsPeople == lbsPeopleInfo.mLbsPeople)
						{
							lbsPeopleInfo.GoBack();
							return;
						}
					}
					object[] object_ = new object[]
					{
						lbsPeopleInfo.mLbsPeople
					};
					ToolFunction.GotoPageForUserControl("/Source/Plugin/Sns/Album/UI/SnsUserMainPage.xaml", object_, false);
					return;
				}
			}
			else
			{
				if (rootPage is ShakePeopleInfo)
				{
					ShakePeopleInfo shakePeopleInfo = rootPage as ShakePeopleInfo;
					if (shakePeopleInfo != null && shakePeopleInfo.shakePeople != null)
					{
						if (prevPage is SnsUserMainPage)
						{
							SnsUserMainPage snsUserMainPage2 = prevPage as SnsUserMainPage;
							if (snsUserMainPage2.shakePeople == shakePeopleInfo.shakePeople)
							{
								shakePeopleInfo.GoBack();
								return;
							}
						}
						object[] object_2 = new object[]
						{
							shakePeopleInfo.shakePeople
						};
						ToolFunction.GotoPageForUserControl("/Source/Plugin/Sns/Album/UI/SnsUserMainPage.xaml", object_2, false);
						return;
					}
				}
				else
				{
					ContactDetailViewForAll contactDetailViewForAll = rootPage as ContactDetailViewForAll;
					if (contactDetailViewForAll != null && contactDetailViewForAll.mContactDitailViewArgs.mContact != null && prevPage is SnsUserMainPage)
					{
						SnsUserMainPage snsUserMainPage3 = prevPage as SnsUserMainPage;
						if (snsUserMainPage3.contact == contactDetailViewForAll.mContactDitailViewArgs.mContact)
						{
							contactDetailViewForAll.GoBack();
							return;
						}
					}
					object[] object_3 = new object[]
					{
						this.ItemObject
					};
					ToolFunction.GotoPageForUserControl("/Source/Plugin/Sns/Album/UI/SnsUserMainPage.xaml", object_3, false);
				}
			}
		}
		private void RefreshItem()
		{
			if (this.ItemObject == null)
			{
				return;
			}
			this.showParent();
			this.registerEvent();
			this.procSnsInfo(SnsPageMgr.getLastSnsImage(this.ItemObject.strUsrName, true));
		}
		private List<TloMedia> procSnsInfo(List<TloMedia> listMideas)
		{
			if (listMideas != null && listMideas.get_Count() != 0)
			{
				SnsPageMgr.setTimeStamp("ui get image success");
				this.showPics(listMideas);
				return listMideas;
			}
			return null;
		}
		private void showPics(List<TloMedia> listMideas)
		{
			if (listMideas == null)
			{
				this.hideParent();
				return;
			}
			for (int i = 0; i < this.panelPics.get_Children().get_Count(); i++)
			{
				CirclePagePicItem circlePagePicItem = this.panelPics.get_Children().get_Item(i) as CirclePagePicItem;
				if (circlePagePicItem != null)
				{
					if (i < listMideas.get_Count())
					{
						circlePagePicItem.ObjMediaInfo = listMideas.get_Item(i);
						circlePagePicItem.set_Visibility(0);
					}
					else
					{
						circlePagePicItem.set_Visibility(1);
					}
				}
			}
			if (listMideas.get_Count() != 0)
			{
				this.showParent();
			}
		}
		private void showParent()
		{
			if (base.get_Parent() != null)
			{
				Panel panel = base.get_Parent() as Panel;
				if (panel != null)
				{
					panel.set_Visibility(0);
				}
			}
		}
		private void hideParent()
		{
			if (base.get_Parent() != null)
			{
				Panel panel = base.get_Parent() as Panel;
				if (panel != null)
				{
					panel.set_Visibility(1);
				}
			}
		}
		public void Collect()
		{
			this.unRegisterEvent();
			this.ItemObject = null;
			for (int i = 0; i < this.panelPics.get_Children().get_Count(); i++)
			{
				CirclePagePicItem circlePagePicItem = this.panelPics.get_Children().get_Item(i) as CirclePagePicItem;
				if (circlePagePicItem != null)
				{
					circlePagePicItem.set_Visibility(0);
					circlePagePicItem.ObjMediaInfo = null;
					circlePagePicItem.imgContent.set_Source(null);
				}
			}
		}
		public static ProfileAblumCtrl GetOneItem()
		{
			if (ProfileAblumCtrl.sItemsCached != null && ProfileAblumCtrl.sItemsCached.get_Count() != 0)
			{
				ProfileAblumCtrl profileAblumCtrl = Enumerable.First<ProfileAblumCtrl>(ProfileAblumCtrl.sItemsCached);
				ProfileAblumCtrl.sItemsCached.RemoveAt(0);
				if (profileAblumCtrl.get_Parent() == null)
				{
					return profileAblumCtrl;
				}
			}
			return new ProfileAblumCtrl();
		}
		public static int GetCacheCnt()
		{
			if (ProfileAblumCtrl.sItemsCached == null)
			{
				return 0;
			}
			return ProfileAblumCtrl.sItemsCached.get_Count();
		}
		public static void PushOneItem(ProfileAblumCtrl item)
		{
			if (item == null)
			{
				return;
			}
			if (ProfileAblumCtrl.sItemsCached == null)
			{
				ProfileAblumCtrl.sItemsCached = new List<ProfileAblumCtrl>();
			}
			if (item.get_Parent() != null)
			{
				Panel panel = item.get_Parent() as Panel;
				if (panel != null)
				{
					panel.get_Children().Remove(item);
				}
			}
			item.Collect();
			if (ProfileAblumCtrl.sItemsCached.get_Count() >= 3)
			{
				return;
			}
			ProfileAblumCtrl.sItemsCached.Add(item);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Contact/ProfileInfo/ProfileAblumCtrl.xaml", 2));
			this.btnLayoutRoot = (Button)base.FindName("btnLayoutRoot");
			this.panelPics = (StackPanel)base.FindName("panelPics");
			this.pic1 = (CirclePagePicItem)base.FindName("pic1");
			this.pic2 = (CirclePagePicItem)base.FindName("pic2");
			this.pic3 = (CirclePagePicItem)base.FindName("pic3");
		}
	}
}
